package com.btb.adapter.utils;

import java.io.IOException;
import java.math.BigDecimal;

import org.web3j.crypto.CipherException;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Wallet;
import org.web3j.crypto.WalletFile;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Transfer;
import org.web3j.utils.Convert;

import com.fasterxml.jackson.databind.ObjectMapper;

public class CredentialsUtils {
	/**
	 * 加载钱包对象(Credentials)
	 *
	 * 四种方式
	 *
	 * 1.通过私钥
	 *
	 * 2.通过密钥对
	 *
	 * 3.通过密码+keyStore路径
	 *
	 * 4.通过密码+keyStore字符串
	 */
	public static Credentials loadCredentials(String privateKey) {
		return Credentials.create(privateKey);
	}

	public static Credentials loadCredentials(ECKeyPair keyPair) {
		return Credentials.create(keyPair);
	}

	public static Credentials loadCredentials(String password, String keyStoreUrl) throws IOException, CipherException {
		return WalletUtils.loadCredentials(password, keyStoreUrl);
	}

	public static Credentials loadCredentialsByString(String password, String keyStoreJson)
			throws IOException, CipherException {
		WalletFile walletFile = new ObjectMapper().readValue(keyStoreJson, WalletFile.class);
		return Credentials.create(Wallet.decrypt(password, walletFile));
	}


}
